package com.apexsoft.monitor.front.console.index.controller;

import com.alibaba.fastjson.JSONObject;
import com.apexsoft.monitor.front.console.aop.AopAnnotation;
import com.apexsoft.monitor.front.console.user.service.UserService;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.session.UserSessionNoRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller("LoginController")
@RequestMapping("/")
public class LoginController extends BaseFrontController {

	@Autowired
	private UserService userSvc = null;
	/**
	 * 登录首页
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/")
	public String loginIndex(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		String loginId = "";
		Cookie cookies[] = request.getCookies();
		for (int i = 0; cookies != null && i < cookies.length; i++) {
			if ("loginId".equals(cookies[i].getName())) {
				loginId = cookies[i].getValue();
			}
		}
		request.setAttribute("loginId", loginId);
		return "console/index/login";
	}
	
	/**
	 * 执行登陆
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 * @throws Exception
	 */
	@AopAnnotation(description="用户登录")
	@RequestMapping(value = "/execLogin")
	public String execLogin(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		String password = request.getParameter("password")==null?"":request.getParameter("password");
		String username = request.getParameter("username")==null?"":request.getParameter("username");
		
		JSONObject result = new JSONObject();
		JSONObject	userObj = userSvc.getSystemUser(username);
		if (userObj.getBooleanValue("success")) {
			JSONObject obj = userObj.getJSONObject("obj");
			if (obj.getString("password").equals(password)) {
				UserSessionNoRedis.setSessionObj(request, UserSessionNoRedis.CONSOLE_SESSION, obj);
				result.put("success", true);
				result.put(Result.NOTE, "登录成功");
				
				// 保存客户号到cookie
				Cookie cookie0 = new Cookie("loginId", obj.getString("loginId"));
				cookie0.setMaxAge(7 * 60 * 60 * 24); // 有效期1天
				response.addCookie(cookie0);
			}else {
				result.put("success", false);
				result.put(Result.NOTE, "口令不正确");
			}
		}else {
			result.put("success", false);
			result.put(Result.NOTE, userObj.getString(Result.NOTE));
		}
		this.printJSON(response, result);
		return null;
		
	}
	
	/*
	 * 登出操作
	 */
	@AopAnnotation(description="注销登录")
	@RequestMapping(value = "/logout")
	public String logout(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		UserSessionNoRedis.removeSession(request, UserSessionNoRedis.CONSOLE_SESSION);
		return "redirect:/";
	}
	
	/*
	 * 超时页面*/
	 
	@RequestMapping(value = "/timeout")
	public String timeout(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		UserSessionNoRedis.removeSession(request, UserSessionNoRedis.CONSOLE_SESSION);
		return "timeout";
	}
	
	@RequestMapping(value = "/404")
	public String error404(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "404";
	}
	
	@RequestMapping(value = "/error")
	public String error(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "error";
	}
	@RequestMapping(value = "/permission")
	public String permission(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "permission";
	}
}
